<%@page import="java.awt.print.Printable"%>
<%@ page contentType="text/html;charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@ taglib prefix="tags" tagdir="/WEB-INF/tags"%>
<c:set var="ctx" value="${pageContext.request.contextPath}" />
<html>
<head>
<meta HTTP-EQUIV="pragma" CONTENT="no-cache">
<meta HTTP-EQUIV="Cache-Control" CONTENT="no-cache, must-revalidate">
<meta HTTP-EQUIV="expires" CONTENT="0">
<%
	response.setHeader("Cache-Control", "no-store");
	response.setHeader("Pragrma", "no-cache");
	response.setDateHeader("Expires", 0);
%>
<title>称重发货</title>
<script type='text/javascript' src='http://wms.ttyuncang.com/WebSpeech.js'></script>
<script type="text/javascript">
	$(function() {
		$("#txt_weight").focus();
		
		$('#weightLock').on('ifChanged', function(event){
		  if($('#weightLock').prop("checked")) {
		    	$("#txt_weight").attr("readonly","readonly");
		  } else {
		    	$("#txt_weight").removeAttr("readonly");
		  }
		});
	});
	WebSpeech.ready(function() {
		WebSpeech.speak('听到声音了吗？');
	});
	//发货
	function send_weight(id) {
	    var weight = $("#txt_weight").val();
	    $("#msg_weight").html("");
	    if (weight == '') {
			$("#txt_weight").select();
			$("#txt_weight").val('');
			$("#txt_weight").focus();
			$("#msg_weight").html("请录入重量!");
			WebSpeech.speak('无重量');
			// sendState = -1;
			return;
		} else if(isNaN(weight)) {
		    $("#txt_weight").select();
		    $("#txt_weight").val('');
			$("#txt_weight").focus();
			$("#msg_weight").html("请输入正确重量!");
			WebSpeech.speak('无效重量');
			// sendState = -1;
			return;
		}
	  
		var expressNo ;
		var type ;
		var sendState = 1;
		if(id == 3){
			if($("#txt_expressNo").val() == ""){
				if($("#txt_orderNo").val() == ""){
					$("#msg_expressNo").html("请录入运单编号或者订单号!");
					$("#txt_expressNo").focus();
					// sendState = -1;
					return;
				}else{
					id = 2 ;
				}
			}else{
				if($("#txt_orderNo").val() == ""){
					id = 1 ;
				}else{
					$("#msg_expressNo").html("请勿同时输入运单编号订单号!");
					$("#txt_expressNo").focus();
					// sendState = -1;
					return;
				}
			}
		}
		if(id == 2){
		  	expressNo = $("#txt_orderNo").val();
			type = "order" ;
		}
		if(id == 1){
		  	expressNo = $("#txt_expressNo").val();
			type = "express" ;
		}

		if (expressNo == '') {
			if(id == 2){
			  	$("#txt_orderNo").select();
				$("#txt_orderNo").focus();
				// $("#msg_orderNo").html("请录入订单号!");
			}else{
			  	$("#txt_expressNo").select();
				$("#txt_expressNo").focus();
				// $("#msg_expressNo").html("请录入运单编号!");
			}
			// sendState = -1;
			WebSpeech.speak('无单号');
			return;
		}
		
		if(sendState == 1){
		  $.ajax({
				type : 'post',
				url : '${ctx}/sendTask/ajaxSend_weight',
				data : {
					expressNo : expressNo,
					weight : weight,
					id : id
				},
				dataType : 'json',
				success : function(data) {
					var msg = data.msg;
					
					if (msg == 'cancel') {
						$("#msg_"+type+"No").css("color", "red");
						$("#msg_"+type+"No").html("此单是取消单，请处理！");

						//显示订单信息
						var workOrder = data.workOrder;
						showOrderInfo(workOrder);

						$("#txt_weight").focus();
						$("#txt_"+type+"No").val('');
						// $("#txt_"+type+"No").focus();
						WebSpeech.speak('已取消');
					} else if (msg == 'noexist') {
						$("#msg_"+type+"No").css("color", "red");
						$("#msg_"+type+"No").html("不存在此订单！");

						$("#txt_weight").focus();
						$("#txt_"+type+"No").val('');
						// $("#txt_"+type+"No").focus();
						WebSpeech.speak('不存在此订单');
					} else if (msg == 'nostatus') {
						$("#msg_"+type+"No").css("color", "red");
						$("#msg_"+type+"No").html("不在发货状态！");

						//显示订单信息
						var workOrder = data.workOrder;
						showOrderInfo(workOrder);

						$("#txt_weight").focus();
						$("#txt_"+type+"No").val('');
						// $("#txt_"+type+"No").focus();
						WebSpeech.speak('错误');
					} else if (msg == 'sended') {
						$("#msg_"+type+"No").css("color", "red");
						$("#msg_"+type+"No").html("此单已经发货！");
						//显示订单信息
						var workOrder = data.workOrder;
						showOrderInfo(workOrder);

						$("#txt_weight").focus();
						$("#txt_"+type+"No").val('');
						// $("#txt_"+type+"No").focus();
						WebSpeech.speak('重复发货');
					} else if (msg == 'nosetting') {
						$("#msg_"+type+"No").css("color", "red");
						$("#msg_"+type+"No").html("无重量配置，请选择称重发货！");

						//显示订单信息
						var workOrder = data.workOrder;
						showOrderInfo(workOrder);

						$("#txt_weight").focus();
						$("#txt_"+type+"No").val('');
						// $("#txt_"+type+"No").focus();
						WebSpeech.speak('无配置');
					} else if (msg == 'multi') {
						$("#msg_"+type+"No").css("color", "red");
						$("#msg_"+type+"No").html("多件商品，请选择称重发货!");

						//显示订单信息
						var workOrder = data.workOrder;
						showOrderInfo(workOrder);

						$("#txt_weight").focus();
						$("#txt_"+type+"No").val('');
						// $("#txt_"+type+"No").focus();
					} else if (msg == 'fail') {
						$("#msg_"+type+"No").css("color", "red");
						$("#msg_"+type+"No").html("该订单回传订单号失败，请处理");

						//显示订单信息
						var workOrder = data.workOrder;
						showOrderInfo(workOrder);

						$("#txt_weight").focus();
						$("#txt_"+type+"No").val('');
						// $("#txt_"+type+"No").focus();
						WebSpeech.speak('失败');
					} else if (msg == 'invoice') {
						$("#msg_"+type+"No").css("color", "red");
						$("#msg_"+type+"No").html("该订单需打印发票，请处理");

						//显示订单信息
						var workOrder = data.workOrder;
						showOrderInfo(workOrder);

						$("#txt_weight").focus();
						$("#txt_"+type+"No").val('');
						// $("#txt_"+type+"No").focus();
					} else if (msg == 'guaranteeStatus') {
						$("#msg_"+type+"No").css("color", "red");
						$("#msg_"+type+"No").html("该订单需打印保修单，请处理");

						//显示订单信息
						var workOrder = data.workOrder;
						showOrderInfo(workOrder);
					} else if (msg == 'success') {
						//显示订单信息
						var workOrder = data.workOrder;
						showOrderInfo(workOrder);
						
						$("#txt_weight").focus();
						$("#txt_"+type+"No").val('');
						// $("#txt_"+type+"No").focus();

						$("#msg_"+type+"No").css("color", "green");
						$("#msg_"+type+"No").html("验证成功，请发货...");
						WebSpeech.speak('成功');
					} else {
					    // 非上面状态
					    $("#txt_weight").focus();
						$("#txt_"+type+"No").val('');
						$("#msg_"+type+"No").css("color", "red");
						$("#msg_"+type+"No").html("系统异常");
					}
					
					if(!$('#weightLock').prop("checked")) {
					  $('#txt_weight').val('');
					  console.log('设置为空');
					}else{
					  $("#txt_"+type+"No").focus();
					  console.log("不设置为空");
					}

				},
				error : function() {
					alert("网络繁忙，请稍后重试...");
				}
			});
		}
		
	}

	function showOrderInfo(workOrder) {
		$("[name='exist']").show();
		$("[name='noexist']").hide();

		$("#orderno").html(workOrder.order.ordeno);
		if(workOrder.expressNo == ""){
			$("#expressNo").html(workOrder.ebillNo);
		}else{
			$("#expressNo").html(workOrder.expressNo);
		}
		$("#expressName").html(workOrder.order.cacode);
		$("#address").html(workOrder.order.addres);
		$("#area").html(
				workOrder.order.provna + "/" + workOrder.order.cityna + "/"
						+ workOrder.order.distna);
		$("#postco").html(workOrder.order.postco);
		$("#custna").html(workOrder.order.recena);
		$("#mobile").html(workOrder.order.mobile);
		$("#teleph").html(workOrder.order.teleph);

		var orderType = workOrder.order.orderType;
		if (orderType == 'single') {
			$("#orderType").html("单件");
		} else if (orderType == 'multiple') {
			$("#orderType").html("多件");
		}

		var billType = workOrder.billType;
		if (billType == 'cbill') {
			$("#billType").html("普通运单");

		} else if (billType == 'ebill') {
			$("#billType").html("电子运单");
		}

		var flowType = workOrder.flowType;
		if (flowType == 'prePrint') {
			$("#flowType").html("预先打单");
		} else if (flowType == 'intimePrint') {
			$("#flowType").html("及时打单");
		}

		var status = workOrder.status;
		if (status == '1') {
			$("#status").html("创建批次");
		} else if (status == '2') {
			$("#status").html("创建任务");
		} else if (status == '3') {
			$("#status").html("已拣货");
		} else if (status == '4') {
			$("#status").html("已灯光拣选");
		} else if (status == '5') {
			$("#status").html("已包装");
		} else if (status == '6') {
			$("#status").html("发货");
		} else if (status == '7') {
			$("#status").html("已签收");
		} else if (status == '8') {
			$("#status").html("异常");
		} else if (status == '9') {
			$("#status").html("待包装");
		}
	}

	function keydown() {

		$("[name='exist']").hide();
		$("[name='noexist']").show();

		$("#msg_expressNo").html('');
		$("#msg_orderNo").html('');

	}
	
</script>
</head>

<body>
	<section class="content">
		<div class="row">
			<div class="col-md-12">
				<!-- Primary box -->
				<div class="box box-solid box-primary">
					<div class="box-header">
						<i class="fa fa-search"></i>
						<h3 class="box-title">扫描发货</h3>
					</div>
					<div class="box-body">
						<div class="form-group row">
							<!-- <div class="col-sm-3">
								<div class="input-textgroup col-sm-12">
									<span class="input-group-text"> 运单编号：</span> <input type="text"
										class="form-control" id="txt_expressNo"
										onchange="send_weight(1);" onkeydown="keydown();" /> 
									<span class="input-group-text"> 订单号：</span> <input type="text"
										class="form-control" id="txt_orderNo"
										onchange="send_weight(2);" onkeydown="keydown();" /> 
									<span class="input-group-text"> 运单重量：</span> <input type="text"
										class="form-control" id="txt_weight" onchange="send_weight(3);"
										onkeydown="keydown();" />
								</div>
							</div>
							<div class="col-sm-3">
								<div class="input-group col-sm-12">
									<span style="color: red; font-weight: bold;" id="msg_expressNo"></span>
								</div>
							</div>
							<div class="col-sm-3">
								<div class="input-group col-sm-12">
									<span style="color: red; font-weight: bold;" id="msg_orderNo"></span>
								</div>
							</div> -->
							<div class="col-sm-3">
								<div class="input-textgroup col-sm-12" style="float: left;">
									<span class="input-group-text"> 运单重量：</span>
									<input type="text" class="form-control" id="txt_weight" onchange="send_weight(3);" onkeydown="keydown();"/>
									<div class="input-group-addon" style="background-color: #FFF; border: none;">
										<input type="checkbox" name="weightLock" id="weightLock"/> <font style="font-weight:bold; font-size: 16px">锁定</font>							
									</div>
								</div>
								
							</div>
							<div class="col-sm-3">
								<div class="input-textgroup col-sm-12">
									<span class="input-group-text"> 运单编号：</span> <input type="text"
										class="form-control" id="txt_expressNo"
										onchange="send_weight(1);" onkeydown="keydown();" />
								</div>
							</div>
							<div class="col-sm-3">
								<div class="input-textgroup col-sm-12">
									<span class="input-group-text"> 订单号：</span> <input type="text"
										class="form-control" id="txt_orderNo"
										onchange="send_weight(2);" onkeydown="keydown();" />
								</div>
							</div>
							<div class="col-sm-3">
								<div class="input-group col-sm-12">
									<span style="color: red; font-weight: bold;" id="msg_expressNo"></span>
								</div>
							</div>
							<div class="col-sm-3">
								<div class="input-group col-sm-12">
									<span style="color: red; font-weight: bold;" id="msg_orderNo"></span>
								</div>
							</div>
							<div class="col-sm-3">
								<div class="input-group col-sm-12">
									<span style="color: red; font-weight: bold;" id="msg_weight"></span>
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>


		<div class="form-group row">
			<div class="col-md-12">
				<div class="box box-solid box-primary">
					<div class="box-header">
						<i class="fa fa-list"></i>
						<h3 class="box-title">运单信息</h3>
						<div class="box-tools pull-right">
							<button class="btn btn-primary btn-sm" data-widget="collapse"
								type="button">
								<i class="fa fa-minus"></i>
							</button>
						</div>
					</div>
					<div class="box-body">
						<table id="contentTable"
							class="table table-striped table-bordered table-condensed">
							<tr name="noexist">
								<td colspan="10" height="50"
									style="vertical-align: middle; text-align: center; color: gray; font-size: 20px;">无数据</td>
							</tr>
							<thead name="exist" style="display: none;">
								<tr>
									<th>运单编号</th>
									<th>快递公司</th>
									<th>省、市、区</th>
									<th>收货人</th>
									<th>联系手机</th>
									<th>联系座机</th>
									<th>收货人地址</th>
									<th>订单状态</th>
								</tr>
							</thead>
							<tbody>
								<tr name="exist" style="display: none;">
									<td id="expressNo"></td>
									<td id="expressName"></td>
									<td id="area"></td>
									<td id="custna"></td>
									<td id="mobile"></td>
									<td id="teleph"></td>
									<td id="address"></td>
									<td id="status"></td>
								</tr>
							</tbody>
						</table>
					</div>
				</div>
			</div>
		</div>
	</section>
</body>
</html>
